﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using cbmobile.DAC;
using PQT.API;
using System.Data;
using cbmobile.BLC;
using System.Data.SqlClient;

namespace cbmobile.BLC
{
    public class BLC_LoaiPhuKien
    {
        private CBMobileEntities da = null;
        public BLC_LoaiPhuKien()
        {
            da = new CBMobileEntities();
        }

        public IList<LoaiPhuKien> All_LoaiPhuKien()
        {
            return da.LoaiPhuKiens.ToList();
        }


        public LoaiPhuKien Get_LoaiPhuKien_By_ID(int Ma_LoaiPhuKien)
        {
            IList<LoaiPhuKien> list = da.LoaiPhuKiens.Where(z => z.Ma_LoaiPhuKien == Ma_LoaiPhuKien).ToList();
            if (list.Count() == 0)
                return null;
            return list.First();
        }

        public IList<LoaiPhuKien> Row_LoaiPhuKien(string p_name)
        {

            IList<LoaiPhuKien> list = da.LoaiPhuKiens.Where(z => z.TenLoaiPhuKien.Contains(p_name)).ToList();
            return list;
        }

        public LoaiPhuKien Get_PK_By_NamePK(string TenLoai_PK)
        {
            IList<LoaiPhuKien> list = da.LoaiPhuKiens.Where(z => z.TenLoaiPhuKien.Contains(TenLoai_PK)).ToList();
            if (list.Count() == 0)
                return null;
            return list.First();
        }

        public IList<LoaiPhuKien> Get_PK_By_IDMaPK(int MaPK)
        {
            IList<LoaiPhuKien> list = da.LoaiPhuKiens.Where(z => z.Ma_LoaiPhuKien == MaPK).ToList();
            if (list.Count() == 0)
                return null;
            return list;
        }

        public int GetTotalRecord(string p_name)
        {
            return da.LoaiPhuKiens.Where(z => z.TenLoaiPhuKien.Contains(p_name)).Count();
        }

        public void Delete(int Ma_LoaiPhuKien)
        {
            var existing = da.LoaiPhuKiens.Where(p => p.Ma_LoaiPhuKien == Ma_LoaiPhuKien).First();
            existing.TrangThai = (int)CommonStatus.Delete;
            da.SaveChanges();
        }


        #region create
        public void Create(int Parent_ID, string TenLoaiPhuKien,int ThuTu, int TrangThai)
        {
            var objEnt = new LoaiPhuKien();
            objEnt.TenLoaiPhuKien = TenLoaiPhuKien;
            objEnt.TrangThai = TrangThai;
            objEnt.Parent_ID = Parent_ID;
            objEnt.ThuTu = ThuTu;

            da.LoaiPhuKiens.AddObject(objEnt);
            da.SaveChanges();
        }
        #endregion

        #region update
        public void Update(int Ma_LoaiPhuKien, int Parent_ID, string TenLoaiPhuKien, int ThuTu, int TrangThai)
        {
            var role = new LoaiPhuKien();
            var objEnt = da.LoaiPhuKiens.Where(p => p.Ma_LoaiPhuKien == Ma_LoaiPhuKien).First();
            objEnt.TenLoaiPhuKien = TenLoaiPhuKien;
            objEnt.TrangThai = TrangThai;
            objEnt.Parent_ID = Parent_ID;
            objEnt.ThuTu = ThuTu;

            da.SaveChanges();
        }
        #endregion

        public void Create(PhuKien role)
        {
            da.PhuKiens.AddObject(role);
            da.SaveChanges();
        }
    }
}